<?php

namespace GamesBundle\Controller;

use GamesBundle\Entity\GamesSpec;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

/**
 * Gamesspec controller.
 *
 */
class GamesSpecController extends Controller
{
    /**
     * Lists all gamesSpec entities.
     *
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $gamesSpecs = $em->getRepository('GamesBundle:GamesSpec')->findAll();

        return $this->render('gamesspec/index.html.twig', array(
            'gamesSpecs' => $gamesSpecs,
        ));
    }

    /**
     * Creates a new gamesSpec entity.
     *
     */
    public function newAction(Request $request)
    {
        $gamesSpec = new Gamesspec();
        $form = $this->createForm('GamesBundle\Form\GamesSpecType', $gamesSpec);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($gamesSpec);
            $em->flush($gamesSpec);

            return $this->redirectToRoute('gamesspec_show', array('id' => $gamesSpec->getId()));
        }

        return $this->render('gamesspec/new.html.twig', array(
            'gamesSpec' => $gamesSpec,
            'form' => $form->createView(),
        ));
    }

    /**
     * Finds and displays a gamesSpec entity.
     *
     */
    public function showAction(GamesSpec $gamesSpec)
    {
        $deleteForm = $this->createDeleteForm($gamesSpec);

        return $this->render('gamesspec/show.html.twig', array(
            'gamesSpec' => $gamesSpec,
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Displays a form to edit an existing gamesSpec entity.
     *
     */
    public function editAction(Request $request, GamesSpec $gamesSpec)
    {
        $deleteForm = $this->createDeleteForm($gamesSpec);
        $editForm = $this->createForm('GamesBundle\Form\GamesSpecType', $gamesSpec);
        $editForm->handleRequest($request);

        if ($editForm->isSubmitted() && $editForm->isValid()) {
            $this->getDoctrine()->getManager()->flush();

            return $this->redirectToRoute('gamesspec_edit', array('id' => $gamesSpec->getId()));
        }

        return $this->render('gamesspec/edit.html.twig', array(
            'gamesSpec' => $gamesSpec,
            'edit_form' => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Deletes a gamesSpec entity.
     *
     */
    public function deleteAction(Request $request, GamesSpec $gamesSpec)
    {
        $form = $this->createDeleteForm($gamesSpec);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->remove($gamesSpec);
            $em->flush($gamesSpec);
        }

        return $this->redirectToRoute('gamesspec_index');
    }

    /**
     * Creates a form to delete a gamesSpec entity.
     *
     * @param GamesSpec $gamesSpec The gamesSpec entity
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm(GamesSpec $gamesSpec)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('gamesspec_delete', array('id' => $gamesSpec->getId())))
            ->setMethod('DELETE')
            ->getForm()
        ;
    }
}
